package com.cindata

import grails.gorm.transactions.Transactional
import org.springframework.security.access.annotation.Secured

import static org.springframework.http.HttpStatus.*

@Secured(['isAuthenticated()'])
@Transactional
class AccountCityController
{

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max)
    {
        params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
        params.offset = params.offset ? params.offset.toInteger() : 0
        def sort = params['sort']
        def order = params['order']
        def name = params['name']
        def sql = " from Account o where 1=1  "
        if (name)
        {
            sql += " and o.name like '%${name}%'"
        }
        def countSql = " select count(o.id) " + sql
        if (order && sort)
        {
            sql += " order by o.${sort} ${order}"
        }
        else
        {
            sql += "order by o.modifiedDate desc "
        }
        def accountList = Account.executeQuery(sql, [max: params.max, offset: params.offset])
        def accountCount = Account.executeQuery(countSql)
        respond accountList, model: [accountCount: accountCount[0], params: params]
    }

    def show(Account Account)
    {
        def accountCity = AccountCity.findAllByAccount(Account)
        respond Account, model: [params: params, accountCity: accountCity]
    }

    def create(Account account1)
    {

        String accountCitySql = null
        def accountCity = AccountCity.where {
            account == account1
        }.city.list()
        if (accountCity.size > 0)
        {
            String cityIdList = accountCity*.id?.toString()
            accountCitySql = "(" + cityIdList?.substring(1, cityIdList.lastIndexOf("]")) + ")"

        }
        def sql = "from Area a where a.areaLevel = 2 "
        if (accountCitySql)
        {
            sql += " and a.id not in " + accountCitySql
        }
        def cityList = Area.executeQuery(sql)
        params.account = account1
        params.cityList = cityList
        respond new AccountCity(params), model: [params: params]
    }

    @Transactional
    def deleteCity()
    {

        String accountId = params["accountId"]
        String cityId = params["cityId"]
        def accountCity = AccountCity.findByCityAndAccount(City.findById(cityId), Account.findById(accountId))


        accountCity.delete flush: true

        redirect action: 'index', method: "GET"
    }

    @Transactional
    def save(AccountCity accountCity)
    {

        if (accountCity == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (accountCity.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond accountCity.errors, view: 'create'
            return
        }
        accountCity.account = Account.findById(params['accountId'])
        accountCity.city = City.findById(params['cityId'])

        accountCity.save flush: true
        redirect action: 'index', method: "GET"
    }

    def edit(AccountCity accountCity)
    {
        respond accountCity
    }

    @Transactional
    def update(AccountCity accountCity)
    {
        if (accountCity == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (accountCity.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond accountCity.errors, view: 'edit'
            return
        }

        accountCity.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'accountCity.label', default: 'AccountCity'), accountCity.id])
                redirect accountCity
            }
            '*' { respond accountCity, [status: OK] }
        }
    }

    @Transactional
    def delete(AccountCity accountCity)
    {

        if (accountCity == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        accountCity.delete flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'accountCity.label', default: 'AccountCity'), accountCity.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NO_CONTENT }
        }
    }

    protected void notFound()
    {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'accountCity.label', default: 'AccountCity'), params.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NOT_FOUND }
        }
    }

    @Transactional
    def searchAccount()
    {
        params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
        params.offset = params.offset ? params.offset.toInteger() : 0;

        def name = params["name"]

        String sql = "from Account as u where 1=1"
        if (name)
        {
            sql += " and u.name like '%${name}%'"
        }
        sql += ' order by modifiedDate desc'

        println "sql:" + sql

        def max = params.max
        def offset = params.offset

        def list = Account.findAll(sql, [max: max, offset: offset])
        def list1 = Account.findAll(sql)
        def count = list1.size()

        respond list, model: [accountCount: count, params: params], view: 'index'
    }
}
